package com.fsc.comadapoc1.presentation.panels
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.entities.TradeRequest;
	import com.fsc.comadapoc1.entities.trade.Trade;
	import com.fsc.comadapoc1.events.CreateTradesEvent;
	import com.fsc.comadapoc1.events.LoadPortfolioFundsEvent;
	import com.fsc.comadapoc1.events.PanelChangeEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import flashx.textLayout.operations.CreateDivOperation;
	
	import mx.controls.DateField;
	

	[Event(name="changeState", type="events.PanelChangeEvent")]
	[Event(name="subscribeTrade", type="events.CreateTradesEvent")]
	[ManagedEvents(names="changeState,subscribeTrade")]
	public class SubscribePanelPM extends EventDispatcher
	{
		/** The landing state **/
		public const LANDING_STATE : String = "landingState";
		
		/** The searching state. **/
		public const SEARCHING_STATE : String = "searchingState";
		
		/** The results state. **/
		public const RESULTS_STATE : String = "resultsState";
		
		/** The no results state. **/
		public const NO_RESULTS_STATE : String = "noResultsState";
		
		/**
		 * Keeps track of search term
		 */ 
		private var searchTerm:String;
		
		private var _currentState:String = LANDING_STATE;
		
		[Bindable]
		[Inject]
		public var model:PortfolioModel;
		
		
		
		/**
		 * Get the current state of the presentation model.
		 */
		[Bindable]
		public function get currentState() : String
		{
			return _currentState;
		}
		
		/**
		 * Sets the current state of the presentation model.
		 */
		public function set currentState( state:String ) : void
		{
			_currentState = state;	
		}
		
		/**
		 * Subscribes to trade
		 */ 		
		public function subscribe(subscribeModel:*):void{
	
			
			var now:Date = new Date();
			var tradeRequest:TradeRequest = new TradeRequest();
			tradeRequest.initiated = now;
			tradeRequest.amount = subscribeModel.SubAmmount;
			tradeRequest.notes = subscribeModel.TradeNotes
			tradeRequest.valueDealingDate = DateField.stringToDate(subscribeModel.ExpDealDate, "DD/MM/YYYY");
		
			dispatchEvent(new CreateTradesEvent(CreateTradesEvent.SUBSCRIBE_TRADE,tradeRequest));
	
			
		}
		

		
		public function back():void{
			dispatchEvent(new PanelChangeEvent(PanelChangeEvent.SUBSCRIPTIONS_VIEW_STATE));
		}
		
	}
}